home *** CD-ROM | disk | FTP | other *** search
- Path: oxy.rust.net!usenet
- From: ebennett@rust.net
- Newsgroups: comp.lang.c++
- Subject: Re: Ctors & member methods ?
- Date: Sat, 20 Jan 1996 22:52:44 GMT
- Organization: Rust Net - High Speed Internet in Detroit 810-642-2276
- Message-ID: <4drh56$1i2@oxy.rust.net>
- References: <3100187d.5776685@ixnews7.ix.netcom.com>
- NNTP-Posting-Host: liv-26.rust.net
- X-Newsreader: Forte Free Agent 1.0.82
-
- n4jvp@ix.netcom.com (n4jvp) wrote:
-
- > I have a question concerning ctors. Can a ctor call a member
- >method?
-
- Yes, it is perfectly valid for a constructor to call a method in the
- current class or a base class.
-
- The following, however, is a common mistake made by beginners:
-
- class A
- {
- public:
- A();
- virtual void someFunc();
- };
-
- class B : public A
- {
- public:
- B();
- virtual void someFunc();
- };
-
- A::A()
- {
- someFunc();
- }
-
- int main()
- {
- B *anObject = new B();
- ...
- }
-
- The constructor for B (code not included above) will call the
- constructor for A, which in turn calls the method someFunc. Many
- beginners think that since an instance of class B is being
- constructed, the someFunc() method called from the constructor of A
- will be B::someFunc(). This is _incorrect_. A::someFunc() will be
- called, because the class B has not yet been completely constructed.
- Watch out for this trap.
-
- Earl
-
-
-
-